package com.hm.ibsm.auto.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hm.common.util.UriUtil;
import com.hm.common.util.EncryptUtil.Base64;
import com.hm.ibsm.auto.IbsmProperties;
import com.hm.ibsm.auto.helper.SystemSessionHolder;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月7日 下午6:32:44
 * @version 1.0
 * @describe
 */
@Slf4j
@Aspect
@Component
public class ActionAspectHandler {

	@Autowired
	private IbsmProperties webProperties;

	@Around("execution(* com.hm.ibsm.web.*.**Controller.*(..))")
	public Object process(ProceedingJoinPoint point) throws Throwable {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();

		String uri = request.getRequestURI();
		if (!webProperties.getWhiteList().contains(uri)) {
			try {
				SystemSessionHolder.getUserSession();
			} catch (Exception e) {
				log.error(ActionAspectHandler.class.getName(), e);
				response.sendRedirect("index.do?error=true&result=" + UriUtil.encode(new String(Base64.encode("会话超时,请重新登录".getBytes()))));
				return null;
			}
		}
		return point.proceed();
	}
}
